<?php

namespace app\cps\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Request;
use think\facade\Cache;
use app\common\Error;

class Base extends BaseController
{
    protected $middleware = ['check_cps_login'];

    public function __construct()
    {
        // $header = Request::header();
        // $redis = Cache::store('redis')->handler();
        // $redis->select(3);

        // if (empty($header['token'])) {
        //     return ['code' => Error::FAIL_CODE, 'message' => Error::TOKEN_NOT_EXIXTS];
        // }
        // $data = $redis->get($header['token']);
        // $result = json_decode($data, true);
        // View::assign('cps_official_accounts_data', $result['cps_official_accounts_data']);
    }

    // 渲染页面公共部分
    // public function index()
    // {
    //     return view('public/index');
    // }

    /**
     * 获取用户信息
     * @return mixed
     */
    public function getUserInfo()
    {
        $header = Request::header();
        if (!empty($header['token'])) {
            $token = $header['token'];
        } else if (!empty(Request::param()['token'])) {
            $token = Request::param()['token'];
        } else {
            $token = '';
        }

        if (empty($token)) {
            return redirect(url('cps/login/index'));
            // return ['code' => Error::FAIL_CODE, 'message' => Error::TOKEN_NOT_EXIXTS];
        }

        $redis = Cache::store('redis')->handler();
        $redis->select(3);
        $data = $redis->get($token);

        return json_decode($data, true);
    }
}